<?php
namespace Home\Controller;
use Think\Controller;
class UserController extends Controller
{
    public function chkcode()
    {
        $Verify = new \Think\Verify();
        $Verify->entry();
    }
    public function regist()
    {
        if(IS_POST)
        {
            $model = D('member');
            if($model->create(I('post.'), 1))
            {
                if($model->add())
                {
                    $this->success('注册成功！', U('login'));
                    exit;
                }
            }
            $this->error($model->getError());
        }
        // 设置页面信息
        $this->assign(array(
            '_page_title' => '注册',
            '_page_keywords' => '注册',
            '_page_description' => '注册',
        ));
        $this->display();
    }
    public function login()
    {
        if(IS_POST)
        {
            $model = D('User');
            if($model->validate($model->_login_validate)->create())
            {
                if($model->login())
                {
                    $return = session('returnUrl');
                    if($return)
                    {
                        session('returnUrl', null);
                        $this->success('登录成功！', $return);
                        exit;
                    }
                    $this->success('登录成功！', U('/'));
                    exit;
                }
            }
            $this->error($model->getError());
        }
        // 设置页面信息
        $this->assign(array(
            '_page_title' => '登录',
            '_page_keywords' => '登录',
            '_page_description' => '登录',
        ));
        $this->display();
    }
    public function logout()
    {
        session(null);
        redirect('/');
    }
    public function chkLogin()
    {
        if(session('id'))
        {
            echo json_encode(array(
                'login' => 1,
                'username' => session('username'),
            ));
        }
        else
        {
            echo json_encode(array(
                'login' => 0,
            ));
        }
    }
}